package com.xinzhitong.www.mapper;

import com.xinzhitong.www.pojo.Chapter;
import com.xinzhitong.www.pojo.Comment;
import com.xinzhitong.www.pojo.Course;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 课程相关操作
 *
 * @author 钟伟良
 */
@Repository
public interface CourseMapper {

    /**
     * 通过课程id获取课程价格
     *
     * @param id 课程id
     * @return 课程价格
     */
    @Select("SELECT price FROM course where id=#{id}")
    Double SelectPriceById(Integer id);

    /**
     * 通过课时id获取课时视频
     *
     * @param id 课时id
     * @return 课时视频
     */
    @Select("SELECT video FROM class where id=#{id}")
    String SelectVideoById(Integer id);

    /**
     * 课时id获取课程id
     *
     * @param id 课时id
     * @return 课程id
     */
    @Select("SELECT course_id FROM class where id=#{id}")
    Integer SelectCourseId(Integer id);

    /**
     * 获取当前课程所有章节的No和名字
     *
     * @return 当前课程所有章节的No和名字
     */
    @Select("SELECT name,No FROM chapter where course_id=#{courseId}")
    List<Chapter> selectNameNo(Integer courseId);

    /**
     * 通过章节id获取所有课时的id和名字
     *
     * @param No       章节No
     * @param courseId 课程id
     * @return 课时id和名字
     */

    @Select("SELECT id,name,No FROM class where chapter_No=#{No} and course_id=#{courseId}")
    List<Map<String, Object>> selectIdName(Integer No, Integer courseId);

    /**
     * 通过课程id获取课程简介
     *
     * @param id 课程id
     * @return 课程简介
     */
    @Select("SELECT name,course_introduction,teacher_introduction,for_post,for_crowd FROM course where id=#{id}")
    Course selectIntroduction(Integer id);

    /**
     * 通过工种id找到课程图片，课程名称，课程价格，课程人数，课程标签，课程id，讲师
     *
     * @param id 工种id
     * @return 课程图片，课程名称，课程价格，课程人数，课程标签，课程id，讲师
     */
    @Select(
            "SELECT name,price,picture,teacher,number,teacher,courseId FROM course_all_general where id=#{id}"
    )
    List<Map<String, Object>> selectCourseGeneral(Integer id);

    /**
     * 查找我的课程
     *
     * @param username 用户名
     * @return 课程
     */
    @Select("SELECT number,picture,courseId,teacher,name FROM course_mine_class_number where username=#{username}")
    List<Map<String, Object>> selectCourseMine(String username);

    /**
     * 视频id获取视频分享链接
     *
     * @param id 视频id
     * @return 链接
     */
    @Select("SELECT video FROM class where id=#{id}")
    String selectVideoLink(Integer id);

    /**
     * 插入用户课程评论
     *
     * @param username  用户名
     * @param text      评论信息
     * @param course_id 课程id
     * @return 整型
     */
    @Insert("INSERT INTO user_comment_course(username,course_id,text)VALUES(#{username},#{course_id},#{text})")
    Integer insertCourseComment(String username, Integer course_id, String text);

    /**
     * 查询课程评论
     *
     * @param courseId 课程id
     * @return 评论列表
     */
    @Select("SELECT nickname,text,datetime FROM comment_course where courseId=#{courseId}")
    List<Comment> selectComment(Integer courseId);

    /**
     * 插入用户试看记录
     *
     * @param classId 课时id
     * @return 成功或失败
     */
    @Insert("INSERT INTO user_watched_class(username,class_id)VALUES(#{username},#{classId})")
    Integer insertUserWatched(String username, Integer classId);

    @Select("SELECT id FROM class where id=#{id}")
    Integer selectClassId(Integer id);

    /*
     * 添加一个课程信息
     * @param Integer credentialId, 工种ID
     * @param String  courseName,   课程名称
     * @param String  courseIntro,  课程简介
     * @param Integer price,        课程价格
     * @param String  teacher,      课程老师
     * @param String  teacherIntro, 老师简介
     * @param String  forward,      课程面向人群
     * @param String  forPost       课程面向的岗位
     * @param MultipartFile picture 图片
     *
     * */
    @Insert("INSERT INTO course (id,credential_id,name,price,course_introduction,teacher_introduction,for_crowd,for_post,teacher,picture)"
            +"VALUES(#{id},#{credentialId},#{courseName},#{price},#{courseIntro},#{teacherIntro},#{forward},#{forPost},#{teacher},#{picture})"
    )
    Integer insertACourse(
            Integer id,
            Integer credentialId,
            String  courseName,
            String  courseIntro,
            Integer price,
            String  teacher,
            String  teacherIntro,
            String  forward,
            String  forPost,
            String  picture
    );

    @Insert("insert into good(id) VALUES(NULL)")
    Integer insertAGood();

    @Select("select last_insert_id()")
    Integer selectLastId();

    /*
     * 编辑一个课程信息
     * @param Integer id,           课程ID
     * @param Integer credentialId, 工种ID
     * @param String  courseName,   课程名称
     * @param String  courseIntro,  课程简介
     * @param Integer price,        课程价格
     * @param String  teacher,      课程老师
     * @param String  teacherIntro, 老师简介
     * @param String  forward,      课程面向人群
     * @param String  forPost       课程面向的岗位
     * @param MultipartFile picture 图片
     *
     * */

    @Update("update course set credential_id = #{credentialId},name = #{courseName},course_introduction = #{courseIntro},"
            +"price = #{price},teacher=#{teacher},teacher_introduction=#{teacherIntro},for_crowd=#{forward},"
            +"for_post = #{forPost},picture=#{picture} where id=#{id}")
    Integer editACourse(
            Integer id,
            Integer credentialId,
            String  courseName,
            String  courseIntro,
            Integer price,
            String  teacher,
            String  teacherIntro,
            String  forward,
            String  forPost,
            String  picture
    );

    /*
     *
     * 删除课程
     * @param Integer id 课程id
     * @return 添加成功信息
     * */
    @Delete("delete from course where id = #{id}")
    Integer deleteCourse(Integer id);

    @Delete("delete from good where id = #{id}")
    Integer deleteGoodId(Integer id);

    @Select("Select name,teacher,for_post,picture from course where name LIKE #{word1} and for_post LIKE #{name1}")
    List<Map<String, Object>> SearchCourseGeneral(String name1, String word1);

    @Select("select name,course_introduction,teacher,teacher_introduction,price,for_post,for_crowd,picture"+
            " FROM course where id=#{id}")
    //@Select("SELECT name,course_introduction,teacher,teacher_introduction,price,for_post,for_crowd,picture FROM course where id=#{id}")
    List<Map<String,Object>> getACourseInfo(Integer id);

    /**
     * 查询用户在某个课程观看的课时的数量
     *
     * @param username 用户名
     * @param courseId 课程id
     * @return 观看课时数
     */
    @Select("SELECT COUNT(DISTINCT classId)FROM watched_class_times where username=#{username} and courseId=#{courseId}")
    Integer selectUserWhatched(String username, Integer courseId);

    @Select("select name,price,date,id from course_message where id=#{id} and username=#{username}")
    Map<String, Object> selectCourseMessage(String username, Integer id);
}